package cn.e2win.biz.cyzj;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.e2win.base.entity.SysUser;
import cn.e2win.biz.comm.ViewExcel;
import cn.e2win.biz.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.entity.SysLabel;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.base.json.vo.Result;
import cn.e2win.biz.annotation.PageParam;
import cn.e2win.biz.base.BaseController;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.base.CommonController;
import cn.e2win.biz.base.CommonService;
import cn.e2win.biz.dbo.CyzjEmp;
import cn.e2win.biz.dbo.CyzjOrg;
import org.springframework.web.servlet.ModelAndView;


@Controller
@Scope("request")
@RequestMapping("/cyzj")
public class CyzjController extends BaseController {

	@Autowired
	private CommonController commonController;
	
	@Autowired
	private CyzjService cyzjService;
	@Autowired
	private CommonService commonService;
	
	@Override
	public BaseService initService() {
		return cyzjService;
	}

	@RequestMapping("selfInfoCyzjOrg")
	public String selfInfoCyzjOrg(HttpServletRequest request, ModelMap modelMap){
		Integer id = pager.getCurrUser().getEmpId();
		if(id!=null){
			CyzjEmp emp = (CyzjEmp)cyzjService.getEntity(CyzjEmp.class,id);
			toCyzjEditor(request, emp.getOrgId(), modelMap);
			
		}
		
		return "cyzj/selfInfoCyzjOrg";
	}


	@RequestMapping("toCyzjEditor/{id}")
	public String toCyzjEditor(HttpServletRequest request, @PathVariable(ID) Integer id, ModelMap modelMap){
		CyzjOrg empty = new CyzjOrg(id);
		String clazz = CyzjOrg.class.getSimpleName();
		Map<String, Object> obj = cyzjService.getEntity(empty);
		modelMap.put("cyzjOrg", obj);
		modelMap.put("labelMainList", commonService.getSysLabelList(empty,"labelMain"));
		modelMap.put("labelOfficeList", commonService.getSysLabelList(empty,"labelOffice"));
		
		modelMap.put("allLabelMainList", commonService.getSysLabelList(clazz,"labelMain"));
		modelMap.put("allLabelOfficeList", commonService.getSysLabelList(clazz,"labelOffice"));
		
		modelMap.put("picLoop", commonService.getSysAttachList(empty, "picLoop"));

		return "cyzj/cyzjEditor";
	}
	
	
	
	
	@RequestMapping("editCyzj")
	@ResponseBody
	public Result editCyzj(HttpServletRequest request,@ModelAttribute CyzjOrg obj,@RequestParam(value="delPicIds",required = false)String delPicIds, ModelMap modelMap){
		Result r = new Result();
		r.setCallbackType(Result.CLOSE_CURRENT);
		r.setNavTabId("listView_"+modelMap.get(FUNID));
		try{
			if(obj.getId()!=null) {
				CyzjOrg empty = new CyzjOrg(obj.getId());
				List<SysLabel> sysLabelList = new ArrayList<SysLabel>();
				sysLabelList.addAll(commonService.getSysLabelList(empty, "labelMain"));
				sysLabelList.addAll(commonService.getSysLabelList(empty, "labelOffice"));
				commonService.delLabel(sysLabelList);
				commonService.delPic(delPicIds);
			}
			cyzjService.editCyzj(obj, commonController.getDsRelationList(request, ITEMS + "dsEmpId"));
		} catch (MyException e) {
			logger.error(e);
			r.setStatusCode(Result.FAILED);
			r.setException(e);
		}
		return r;
	}

	@RequestMapping("delLabel")
	@ResponseBody
	public Result delLabel(HttpServletRequest request, Integer id, ModelMap modelMap){
		Result r = new Result();
		try{
			cyzjService.delEntity(SysLabel.class,id);
		} catch (Exception e) {
			r.setException(e);
		}
		return r;
	}



	@RequestMapping("cyzjPager")
	public String cyzjPager(HttpServletRequest request, ModelMap modelMap){

		if(StringUtils.isEmpty(pager.getOrderField())){
			pager.setOrderField("orderInt");
			pager.setOrderDirection("DESC");
			
		}
		
		if("APPROVE".equals(todo)){
			this.pager.getScMap().put("approveStatus", "SUBMITED");
		}else if("EDIT".equals(todo)){
			this.pager.getScMap().put("approveStatus_IN", "UNSUBMIT【】REJECTED");
		}
		setScMap(this.pager);
		cyzjService.pagerCyzj(this.pager);
		pager.setResultList(this.pager.getResultList()) ;
		modelMap.put("pager", this.pager);
		return "cyzj/cyzjPager";
	}

	@RequestMapping("cyzjPagerExcel")
	public ModelAndView customerPagerExcel(HttpServletRequest request, ModelMap modelMap) {
		pager.setReturnAll(true);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("key",cyzjPager(request,modelMap));
		model.put("list", pager.getResultList());
        return new ModelAndView(new ViewExcel(), model);
	}


	private void setScMap(Pager pager){

		SysUser user = pager.getCurrUser();
		String user_I = pager.getScMap().get("user_I");
		
		//我的工作区
		if(!StringUtils.isEmpty(user_I)){
			//外部用户，导师及创业之家用户
			if(user.getUserType().equals("DsEmp") || user.getUserType().equals("CyzjEmp")){
				//领导
				if("1".equals(user.getIsLeader())){
				//普通工作人员
				}else{
				}
			//内部用户，即指导中心用户	
			}else if(user.getUserType().equals("ZdzxEmp")){
				//领导
				if("1".equals(user.getIsLeader())){
					pager.getScMap().remove("user_I");
				//普通工作人员
				}else{
					
				}
			}
		//非我的工作区
		}else{
			//外部用户，导师及创业之家用户
			if(user.getUserType().equals("DsEmp") || user.getUserType().equals("CyzjEmp")){
				//领导
				if("1".equals(user.getIsLeader())){
					
				//普通工作人员
				}else{
					
				}
				pager.getScMap().put("approveStatus", "APPROVED");
			//内部用户，即指导中心用户	
			}else if(user.getUserType().equals("ZdzxEmp")){
				//领导
				if("1".equals(user.getIsLeader())){
				//普通工作人员
				}else{
					//资格审核，资料更新审核， 资料修改
					if("APPROVE".equals(todo) || "MDF_APPROVE".equals(todo)  || "UPDATE".equals(todo)){
						pager.getScMap().put("adminUserId", user.getId()+"");
					//修改
					}else if("EDIT".equals(todo)){
						pager.getScMap().put("createById", user.getId()+"");
					}
					
				}
			}
		}

		
	}
	
	
	

	@RequestMapping("toCyzjEmpEditor/{id}")
	public String toCyzjEmpEditor(HttpServletRequest request, @PathVariable(ID) Integer id, ModelMap modelMap){
		Integer empId = null;
		CyzjEmp cyzjEmp = null;
		if(pager.getCurrUser()!=null){
			empId = pager.getCurrUser().getEmpId();
			if(pager.getCurrUser().getUserType().equals("CyzjEmp")){
				cyzjEmp = (CyzjEmp) cyzjService.getEntity(CyzjEmp.class,empId);
				if(cyzjEmp!=null){
					modelMap.addAttribute("currEmp",cyzjEmp);
				}
			}
		}
		Map<String, Object> obj = cyzjService.getEntity(new CyzjEmp(id));
		modelMap.put(OBJECT, obj);
		return "cyzj/cyzjEmpEditor";
	}

	@RequestMapping("editCyzjEmp")
	@ResponseBody
	public Result editCyzjEmp(HttpServletRequest request,@ModelAttribute CyzjEmp obj,ModelMap modelMap){
		Result r = new Result();
		r.setCallbackType(Result.CLOSE_CURRENT);
		r.setNavTabId("listView_"+modelMap.get(FUNID));
		try{
		cyzjService.editCyzjEmp(obj);
		} catch (Exception e) {
			logger.error(e);
			r.setStatusCode(Result.FAILED);
			r.setException(e);
		}
		return r;
	}


	@RequestMapping("cyzjEmpPager")
	public String cyzjEmpPager(HttpServletRequest request, ModelMap modelMap){
		if("approve".equals(todo)){
			pager.getScMap().put("approveStatus", "SUBMITED");
		}else if("submit".equals(todo)){
			pager.getScMap().put("approveStatus_IN", "'UNSUBMIT','REJECTED'");
		}
		cyzjService.pagerCyzjEmp(pager);
		modelMap.put("pager", pager);
		return "cyzj/cyzjEmpPager";
	}

	@RequestMapping("cyzjEmpPagerExcel")
	public ModelAndView cyzjEmpPagerExcel(HttpServletRequest request, ModelMap modelMap) {
		pager.setReturnAll(true);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("key",cyzjEmpPager(request,modelMap));
		model.put("list", pager.getResultList());
		return new ModelAndView(new ViewExcel(), model);
	}
	
	
	@RequestMapping("toTheMap")
	public String theMap(HttpServletRequest request,
			@PageParam Pager pager, ModelMap modelMap){
		String x=request.getParameter("x");
		String y=request.getParameter("y");
		String address=request.getParameter("address");
		modelMap.put("x", x);
		modelMap.put("y", y);
		try {
			if(address!=null){
				modelMap.put("address", new String(address.getBytes("iso-8859-1"), "utf-8"));
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "cyzj/theMap";
	}
	

	
}
